<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodsController extends Controller
{
    //
    /**
     * 商品的添加页面
     */
    public function getAdd()
    {
        //读取分类信息
        $res = CateController::getCateInfo();
        //解析模版  分配变量
    	return view('admin.good.add',['cates'=>$res]);
    }

    /**
     * 执行添加操作
     */
    public function postInsert(Request $request)
    {
        //提取数据
        $data = $request->except(['_token','path']);

        //拼接内容
        $data['color'] = implode('@@',$data['color']);
        $data['size'] = implode('@@',$data['size']);
        //将数据插入到主表中
        $gid = DB::table('goods')->insertGetId($data);
        //处理图片
        if($request->hasFile('path')) {
            //带拼接的数据
            $d = [];
            //获取所有的文件对象
            $files = $request->file('path');
            foreach($files as $k => $v) {
                $fileName = time().rand(100000,999999);
                $suffix = $v->getClientOriginalExtension();
                //拼接文名
                $fileName = $fileName.'.'.$suffix;
                //目录
                $dir = './Uploads/'.date('Ymd').'/';
                $v->move($dir,$fileName);
                $tmp['path'] = trim($dir.$fileName,'.');
                $tmp['goods_id'] = $gid;
                $d[] = $tmp;
            }
            //将图片的信息插入到附表中
            $res = DB::table('pics')->insert($d);
            if($res && $gid) {
                return redirect('/good/index')->with('info','添加成功');
            }else {
                return back()->with('info','添加失败');
            }
        }
        if($gid) {
            return redirect('/good/index')->with('info','添加成功');
        }else {
            return back()->with('info','添加失败');
        }
    }

    /**
     * 商品列表
     */
    public function getIndex(Request $request)
    {
         // 读取数据
        $goods = DB::table('goods')->orderBy('id','asc')
        ->select('goods.*','cates.name as names')
        ->join('cates','cates.id','=','goods.cate_id')
        ->where(function($query) use ($request) {
            //获取关键字
            $k = $request->input('keyword');
            if(!empty($k)) {
                $query->where('name','like','%'.$k.'%');
            }
        })->paginate($request->input('num',10));
        return view('admin.good.index',['goods'=>$goods,'request'=>$request]);
    }

    /**
     * 商品修改
     */
    public function getEdit(Request $request)
    {
        //读取当前分类信息
        $res = DB::table('goods')->where('id',$request->input('id'))->first();
        //读取所有的分类
        $cates = DB::table('cates')->get();
        // 显示页面
        return view('admin.good.edit',['cates'=>$cates],['info'=>$res]);
    }

    /**
     * 执行修改
     */
    public function postUpdate(Request $request)
    {

        $data = $request->except(['_token','id']);
         //拼接内容
        $data['color'] = implode('@@',$data['color']);
        $data['size'] = implode('@@',$data['size']);
        $res = DB::table('goods')->where('id', $request->input('id'))->update($data);
        if($res) {
            return redirect('/good/index')->with('info','更新成功');
        }else{
            return back()->with('info','更新失败');
        }
    }

    /**
     * 删除
     */
    public function getDelete(Request $request)
    {
        $pid = DB::table('pics')->where('goods_id',$request->id)->delete();
        $res = DB::table('goods')->where('id',$request->id)->delete();

        //
        if($res){
            return redirect('/good/index')->with('info','删除成功!!!');
        }else{
            return redirect('/good/index')->with('info','删除失败!!!');
        }
    }
}

